<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="comment in commentData"
        :key="comment.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="comment.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{comment.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{comment.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{comment.pubdate|relativeTime}}</span>
            <van-button size="mini" type="default" @click="hClickReply(comment)">回复</van-button>
          </p>
        </div>
        <van-icon
        slot="right-icon"
        @click="hAddCommentLike(comment)"
        :name="comment.is_liking ?'like':'like-o'"
        />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="content"
      >
        <van-button @click="hAddComment" slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->

    <!-- 评论回复 -->
    <van-popup
      v-model="isReplyShow"
      round
      position="bottom"
      :style="{ height : '85%'}"
    >
    <comment-reply :comment="cureentComent" :article-id="articleId" v-if="isReplyShow" :reply-show.sync="isReplyShow"></comment-reply>
    </van-popup>
    <!-- /评论回复 -->
  </div>
</template>

<script>
import { addComment, getComment, addCommentLike, deleteCommentLike } from '@/api/comment.js'
import CommentReply from './commentReply'
export default {
  name: 'ArticleComment',
  components: { CommentReply },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      cureentComent: {},
      commentData: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      content: null, // 评论的内容
      offset: null, // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
      count: null,
      isReplyShow: false
    }
  },
  methods: {
    async onLoad () {
      const res = await getComment({ type: 'a', source: this.articleId, offset: this.offset })
      const data = res.data.results
      this.commentData.unshift(...data)
      this.loading = false
      this.finished = !data.length
      this.offset = res.data.last_id
      this.count = res.data.total_count
    },
    async hAddComment () { // 发布评论
      if (!this.content) return
      // target文章id,content评论内容
      const res = await addComment({ target: this.articleId, content: this.content })
      if (!res) return this.$toast.fail('该文章禁止评论')
      this.commentData.unshift(res.data.new_obj)// 将返回的新增评论更新到本地
      this.content = ''
    },
    // 点击喜欢
    async hAddCommentLike (comment) {
      try {
        comment.is_liking ? await deleteCommentLike(comment.com_id.toString()) : await addCommentLike({ target: comment.com_id.toString() })
        comment.is_liking = !comment.is_liking
        this.$toast.success('操作成功')
      } catch (err) {
        console.log('点赞操作失败', err)
      }
    },
    hClickReply (item) { // 点击回复
      this.isReplyShow = true
      this.cureentComent = item
    }
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
